package com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.web;

import java.util.*;

import com.github.pagehelper.PageInfo;
import com.iwhalecloud.citybrain.flow.platform.manager.common.ResponseResult;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.enums.MsgTypeEnum;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.*;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.notice.FlowNoticeSettingsVo;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.repository.FlowIssueNoticeRepository;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.repository.FlowNoticeSettingsRepository;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author lilh
 * @date 2019-04-25 15:55
 * @modify by: yetaorui
 */
@Api(value = "消息通知", tags = "消息通知")
@RestController
@RequestMapping("/notice")
public class FlowIssueNoticeController {

    @Autowired
    private FlowIssueNoticeRepository flowIssueNoticeRepository;

    @Autowired
    private FlowNoticeSettingsRepository flowNoticeSettingsRepository;

    /**
     * 列表
     *
     * @param flowIssueNoticeQueryVo 参数
     * @return 结果
     */
    @ApiOperation("消息列表")
    @PostMapping("/list")
    public ResponseResult list(@RequestBody FlowIssueNoticeQueryVo flowIssueNoticeQueryVo) {
        return flowIssueNoticeRepository.listIssueNotice(flowIssueNoticeQueryVo);
    }

    /**
     * 更新已读状态
     *
     * @param flowIssueNoticeUpdateStatusVo 参数
     * @return 结果
     */
    @ApiOperation("更新已读状态")
    @PostMapping("/updateReadStatus")
    public ResponseResult updateReadStatus(@RequestBody FlowIssueNoticeUpdateStatusVo flowIssueNoticeUpdateStatusVo) {
        return ResponseResult.success(flowIssueNoticeRepository.updateReadStatus(flowIssueNoticeUpdateStatusVo));
    }

    /**
     * 详情
     */
    @ApiOperation("详情")
    @PostMapping("/detail")
    public ResponseResult detail(@RequestBody FlowIssueNoticeDetailVo flowIssueNoticeDetailVo) {
        if (Objects.nonNull(flowIssueNoticeDetailVo.getId())) {
            FlowIssueNoticeUpdateStatusVo flowIssueNoticeUpdateStatusVo = new FlowIssueNoticeUpdateStatusVo();
            flowIssueNoticeUpdateStatusVo.getIds().add(flowIssueNoticeDetailVo.getId());
            flowIssueNoticeRepository.updateReadStatus(flowIssueNoticeUpdateStatusVo);
            return ResponseResult.success(flowIssueNoticeRepository.detail(flowIssueNoticeDetailVo.getId()));
        }
        return ResponseResult.success();
    }

    /**
     * 通知主题列表
     */
    @ApiOperation("通知主题列表")
    @PostMapping("/noticeThemType")
    public ResponseResult noticeThemType() {

        Map<String, Map<String, String>> msgTypeMap = MsgTypeEnum.getCodeToDesc();

        List<MsgTypeVo> msgTypeVoList = new ArrayList<>();

        for (Map.Entry<String, Map<String, String>> entry : msgTypeMap.entrySet()) {

            Map<String, String> value = entry.getValue();
            for (String code : value.keySet()) {
                MsgTypeVo msgTypeVo = new MsgTypeVo();
                msgTypeVo.setType(entry.getKey());
                msgTypeVo.setKey(code);
                msgTypeVo.setValue(value.get(code));
                msgTypeVoList.add(msgTypeVo);
            }

        }

        return ResponseResult.success().setDatas(msgTypeVoList);
    }

    /**
     * 消息主题列表
     */
    @ApiOperation("消息主题列表")
    @PostMapping("/msgThemType")
    public ResponseResult msgThemType() {

        //todo 对接第三方消息主题接口
//        Object templates = flowNoticeSettingsRepository.getTemplates();
//
//        return ResponseResult.success(templates);
        List<MsgThemTypeVo> msgThemTypeVoList = new ArrayList<>();

        MsgThemTypeVo msgThemTypeVo = new MsgThemTypeVo();
        msgThemTypeVo.setTemplateId("message_topic_A");
        msgThemTypeVo.setKey("message_topic_A");
        msgThemTypeVo.setTemplateName("消息主题1");
        msgThemTypeVoList.add(msgThemTypeVo);

        MsgThemTypeVo msgThemTypeVo2 = new MsgThemTypeVo();
        msgThemTypeVo2.setTemplateId("message_topic_B");
        msgThemTypeVo2.setKey("message_topic_B");
        msgThemTypeVo2.setTemplateName("消息主题2");
        msgThemTypeVoList.add(msgThemTypeVo2);

        MsgThemTypeVo msgThemTypeVo3 = new MsgThemTypeVo();
        msgThemTypeVo3.setTemplateId("message_topic_C");
        msgThemTypeVo3.setKey("message_topic_C");
        msgThemTypeVo3.setTemplateName("消息主题3");
        msgThemTypeVoList.add(msgThemTypeVo3);

        return ResponseResult.success().setDatas(msgThemTypeVoList);


    }

    /**
     * 通知列表查询
     */
    @ApiOperation("通知列表查询")
    @PostMapping("/listNoticeSettings")
    public ResponseResult listNoticeSettings(@RequestBody FlowNoticeSettingsVo flowNoticeSettingsVo) {

        PageInfo<FlowNoticeSettingsVo> pageInfo = flowNoticeSettingsRepository.queryAll(flowNoticeSettingsVo);

        return ResponseResult.success(pageInfo.getList()).setTotalCount((int) pageInfo.getTotal()).setPageNum(pageInfo.getPageNum());
    }

    /**
     * 通知列表新增
     */
    @ApiOperation("通知列表新增")
    @PostMapping("/listAdd")
    public ResponseResult listAdd(@RequestBody FlowNoticeSettingsVo flowNoticeSettingsVo) {

        FlowNoticeSettingsVo insert = flowNoticeSettingsRepository.insert(flowNoticeSettingsVo);

        return ResponseResult.success(insert);
    }

    /**
     * 通知列表新增/修改
     */
    @ApiOperation("通知列表修改/删除")
    @PostMapping("/listUpdate")
    public ResponseResult listUpdate(@RequestBody FlowNoticeSettingsVo flowNoticeSettingsVo) {

        FlowNoticeSettingsVo update = flowNoticeSettingsRepository.update(flowNoticeSettingsVo);

        return ResponseResult.success(update);
    }


}
